import { baseRequestClient } from '#/api/request';
import { requestClient } from '#/api/request';

// 查询阀门基本信息列表
// 查询阀门参数
interface PoolItem {
    poolCode?: string;
    poolName?: string;
    valveType?: number;
  }
  // 查询阀门返回值
  interface ListPoolResult {
    rows: any;
    total: number;
  }
  
  //添加阀门参数
  export interface AddPoolParams extends Record<string,any> {
    poolCode: string;
    poolName: string;
    longitude: string;
    latitude: string;
    valveType: number;
    remark: string;
    location:string;
    pipelineCode:string;
    pipeCode:string;
  }
  
  //修改阀门参数
  export interface EditPoolParams extends Record<string,any> {
    poolCode: string;
    poolName: string;
    longitude: string;
    latitude: string;
    valveType: number;
    remark: string;
    location:string;
    pipelineCode:string;
    pipeCode:string;
  }
// 查询集油线阀池列表在地图上展示
export const getValvesByGis = async () => {
  const response = await requestClient.get<ListPoolResult>('/manage/valve/list');
  const data = response.rows; // 提取 rows 字段
  const mappedResult = data.map((row: any) => {
    const location = row.location;
    let locationFormatted: [number,number] | undefined = undefined;
  
    if (location && location.startsWith('POINT')) {
      const [longitude,latitude] = location.match(/-?\d+(\.\d+)?/g)?.map(Number) || [];
      locationFormatted = [longitude,latitude];
    }
    
    return {
      ...row,
      location: locationFormatted,
    };
  });
  return mappedResult;
};

// 查询集油线阀池列表
export const listPool = async (params: {
  pageNum: number;
  pageSize: number;
  poolCode?: string;
  poolName?: string;
  valveType?: string;
}) => {
  const response = await requestClient.get<ListPoolResult>('/manage/valve/list', { params });
  const data = response.rows; // 提取 rows 字段
  let filteredData: PoolItem[] = data;
  // 过滤数据
  if (params.poolCode) {
    filteredData = filteredData.filter((item: PoolItem) =>
      item.poolCode?.includes(params.poolCode ?? '') ?? false
    );
  }
  if (params.poolName) {
    filteredData = filteredData.filter((item: PoolItem) =>
      item.poolName?.includes(params.poolName ?? '') ?? false
    );
  }
  if (params.valveType) {
    const statusNum = parseInt(params.valveType, 10);
    if (!isNaN(statusNum)) {
      filteredData = filteredData.filter((item: PoolItem) => item.valveType === statusNum);
    }
  }

  const total = response.total;
  
  const result = filteredData;

  // 处理数据
  const mappedResult = result.map((row: any) => {
    
    const location = row.location;
    let locationFormatted: string | undefined = undefined;

    if (location && location.startsWith('POINT')) {
      const [longitude, latitude] = location.match(/-?\d+(\.\d+)?/g)?.map(Number) || [];
      locationFormatted = `经度: ${longitude}, 纬度: ${latitude}`; 
    }

    return {
        ...row,
        valveType: getPoolTypeName(row.valveType),
        location: locationFormatted,
      };
    });
    
    // 添加一个辅助函数来获取阀门类型的名称
    function getPoolTypeName(valveType: number): string {
      switch (valveType) {
        case 0:
          return '闸型';
        case 1:
          return '球型';
        case 2:
          return '蝶型';
        default:
          return '未知类型';
      }
    }

  return {
    items: mappedResult,
    total: total,
  };
};

// 查询集油线阀池详细
export async function getPool(id: any) {
  return baseRequestClient.get(`/manage/valve/${id}`);
}

// 新增集油线阀池
export async function addPool(data: AddPoolParams) {
  data.pipeCode = data.pipelineCode;
  await requestClient.post('/manage/valve', data);
}

// 修改集油线阀池
export async function updatePool(data: EditPoolParams) {
  await requestClient.put('/manage/valve', data);
}

// 删除集油线阀池
export async function delPool(id: number) {
  await requestClient.delete(`/manage/valve/${id}`);
}
